﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using TestMySqlConnetPoll;
using System.Threading;
using DevExpress.XtraBars.Alerter;

namespace View.Journal
{
    /// <summary>
    /// 系统工作日志
    /// </summary>
    public partial class ButtonLogForm : DevExpress.XtraEditors.XtraForm
    {
        //系统工作日志
        private ButtenUseTable btnLog = new ButtenUseTable();
        private DataTable bindTable = new DataTable();
        private AlertControl alart = new AlertControl();

        public ButtonLogForm()
        {
            InitializeComponent();
            LoadTimeList();
            //初始化数据绑定表
            bindTable.Columns.Add("name");
            bindTable.Columns.Add("time_s");
            bindTable.Columns.Add("time_e");
            bindTable.Columns.Add("status");
            bindTable.Columns.Add("time_total");

            //设置日志类型初始选择

            if (!ReadValue())
            {
                if (alart.AlertFormList.Count > 0)
                {
                    alart.AlertFormList[0].Close();
                }
                alart.Show(this, "没有查询到数据", "");
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnS_Click(object sender, EventArgs e)
        {
            if(!ReadValue())
            {
                if (alart.AlertFormList.Count > 0)
                {
                    alart.AlertFormList[0].Close();
                }
                alart.Show(this, "没有查询到数据", "");
            }
        }

        /// <summary>
        /// 初始化时间
        /// </summary>
        private void LoadTimeList()
        {
            btnLog.ReadTimeList();

            while (!btnLog.Ready_TimeList)
            {
                if (btnLog.ErrorFlag != 0)
                {
                    return ;
                }

                Thread.Sleep(20);
            }

            foreach(objDate date  in btnLog.TimeList)
            {
                this.comboBox_time.Items.Add(date);
            }

            if (btnLog.TimeList.Count > 0)
            {
                this.comboBox_time.SelectedIndex = 0;
            }
        }

        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="date"></param>
        /// <param name="strLogType"></param>
        private bool ReadValue()
        {
            bindTable.Rows.Clear();

            btnLog.ReadValueList((objDate)this.comboBox_time.SelectedItem);

            while (!btnLog.Ready_ValueList)
            {
                if (btnLog.ErrorFlag != 0)
                {
                    return false;
                }
                Thread.Sleep(20);
            }

            foreach (KeyValuePair<string, List<objButton>> keyvalue in btnLog.ValueList)
            {
                string strName = keyvalue.Key;

                foreach (objButton objBtn in keyvalue.Value)
                {
                    string strTime_s;
                    string strTime_e;
                    string strStatus = "--";
                    string strTimeTotal;

                    if (objBtn.s_time == null)
                        strTime_s = "--";
                    else
                        strTime_s = MySql.Time_tToDateTime(objBtn.s_time).ToLongTimeString();

                    if (objBtn.e_time == null)
                        strTime_e = "--";
                    else
                        strTime_e = MySql.Time_tToDateTime(objBtn.e_time).ToLongTimeString();

                    if (objBtn.e_time == null && objBtn.s_time != null)
                        strStatus = "↓";
                    else if (objBtn.e_time != null && objBtn.s_time != null)
                        strStatus = "↑";

                    if (objBtn.s_time != null && objBtn.e_time != null)
                        strTimeTotal = MySql.Time_tToDateTime(objBtn.e_time).Subtract(MySql.Time_tToDateTime(objBtn.s_time)).TotalSeconds.ToString();
                    else
                        strTimeTotal = "--";

                    bindTable.Rows.Add(new object[] { strName, strTime_s, strTime_e, strStatus, strTimeTotal });
                }
            }

            foreach (c_Device.objButtenStruct btnStruct in c_Device.ButtenUseConfig)
            {
                if (!btnLog.ValueList.ContainsKey(btnStruct.devName))
                {
                    bindTable.Rows.Add(new object[] { btnStruct.devName, "--", "--", "--", "--" });
                }

            }

            this.grid_main.DataSource = bindTable;

            if (bindTable.Rows.Count == 0)
                return false;

            

            return true;
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnP_Click(object sender, EventArgs e)
        {
            grid_main.ShowRibbonPrintPreview();
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnE_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog();
            sf.Filter = "Excel(*.xlsx)|*.xlsx";
            sf.FileName = "控制台按钮运用记录";

            if (sf.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                grid_main.ExportToXlsx(sf.FileName);
            }
        }

        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnC_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //private void DateEdit_S_EditValueChanged(object sender, EventArgs e)
        //{
        //    if (((DateTime)dateEdit_S.EditValue).CompareTo((DateTime)dateEdit_E.EditValue) > 0)
        //    {
        //        dateEdit_S.EditValue = dateEdit_E.EditValue;
        //    }
        //}

        //private void DateEdit_E_EditValueChanged(object sender, EventArgs e)
        //{
        //    if (((DateTime)dateEdit_S.EditValue).CompareTo((DateTime)dateEdit_E.EditValue) > 0)
        //    {
        //        dateEdit_E.EditValue = dateEdit_S.EditValue;
        //    }
        //}
    }
}